package com.qianchang.microfinance.view.dialog;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.model.IPickerViewData;
import com.qianchang.microfinance.R;
import com.dm.library.utils.ToastUtil;

import java.util.List;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: <单项item轮播选择框，bean类必须实现IPickerViewData接口>
 * 修 改 人: sushuo
 * 修改时间: 2017-3-21
 */

public class CommonDialog<T extends IPickerViewData> extends BaseDialog {

    private Context context;
    private List<T> beanList;
    private String title;
    private int flag;

    private OptionsPickerView pvOptions;
    private OnItemSelectListener onItemSelectListener;


    public CommonDialog(Context context, List<T> beanList) {
        this.context = context;
        this.beanList = beanList;
    }


    public CommonDialog(Context context, String title, List<T> beanList, int flag, OnItemSelectListener onItemSelectListener) {
        this.context = context;
        this.beanList = beanList;
        this.title = title;
        this.flag = flag;
        this.onItemSelectListener = onItemSelectListener;
    }


    public void setTitle(String title) {
        this.title = title;
    }

    public void showPickerView() {
        if (null == beanList || beanList.size() == 0) {
            ToastUtil.getInstant().show(context, context.getString(R.string.data_error));
            return;
        }
        pvOptions = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (onItemSelectListener != null) {
                    onItemSelectListener.onItemSelect(beanList.get(options1), options1, flag);
                }
            }
        }).setTitleColor(context.getResources().getColor(R.color.common_black_color_3))
                .setTitleText(this.title)
                .setDividerColor(context.getResources().getColor(R.color.base_line_color))
                .setTextColorCenter(context.getResources().getColor(R.color.common_black_color_3)) //设置选中项文字颜色
                .setCancelColor(context.getResources().getColor(R.color.common_black_color_6))
                .setContentTextSize(16)
                .setOutSideCancelable(false)// default is true
                .setSubmitText(context.getString(R.string.confirm))
                .setCancelText(context.getString(R.string.cancel))
                .setSubCalSize(14)
                .build();
        pvOptions.setPicker(beanList);
        View currentFocus = ((Activity) context).getCurrentFocus();
        if (null != currentFocus) {
            ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
        pvOptions.show();
    }

    public void setOnItemSelectListener(OnItemSelectListener onItemSelectListener) {
        this.onItemSelectListener = onItemSelectListener;
    }

    public interface OnItemSelectListener<T extends IPickerViewData> {
        void onItemSelect(T t, int position, int flag);
    }

    public static Builder init() {
        return new Builder();
    }

    public static class Builder {
        private Context context;
        private List beanList;
        private String title;
        private int flag;
        private OnItemSelectListener onItemSelectListener;

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public <T extends IPickerViewData> Builder setData(List<T> beanList) {
            this.beanList = beanList;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setFlag(int flag) {
            this.flag = flag;
            return this;
        }

        public Builder setOnItemSelectListener(OnItemSelectListener onItemSelectListener) {
            this.onItemSelectListener = onItemSelectListener;
            return this;
        }


        public CommonDialog build() {
            return new CommonDialog(context, title, beanList, flag, onItemSelectListener);
        }


    }

}
